import instacne from "../request";
import type { Ref } from "vue";

/**
 * 登录返回数据
 */
interface IAdminLoginParams {
  password: string;
  username: string;
}
interface IAdminLoginRes {
  tokenHead: string;
  token: string;
}

const adminLogin = (data: IAdminLoginParams): Promise<IAdminLoginRes> => {
  // instacne.post("/auth/login", data); 是一个 Promise 类型
  return instacne.post("/admin/login", data);
};

/**
 * 获取当前登录用户信息
 */
interface IAdminInfoRes {
  menus: IMneuItem[];
  icon: string;
  username: string;
  roles: string[];
}
const getAdminInfo = (): Promise<IAdminInfoRes> => {
  return instacne.get("/admin/info");
};

/**
 * /admin/list 根据用户名或者姓名获取用户列表
 */
interface IAdminListParams {
  keyword?: string | Ref<string>;
  pageNum?: number | Ref<number>;
  pageSize?: number | Ref<number>;
}

interface IAdminListItem {
  createTime: string;
  email: string;
  icon: string;
  id: number;
  loginTime: string;
  nickName: string;
  note: string;
  password: string;
  status: number;
  username: string;
}

interface IAdminListRes {
  list: IAdminListItem[];
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}
//分配资源约束
interface IResources {
  id: number;
  createTime: string;
  name: string;
  url: string;
  description: string;
  categoryId: number;
}
const getAdminList = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get("/admin/list", {
    params: data,
  });
}; //分配资源
const getresourcesRole = (data: string): Promise<IResources> => {
  return instacne.get(`role/listResource/${data}`);
};

//全部资源
const getresources = (): Promise<IResources> => {
  return instacne.get("resource/listAll");
};

/**
 * 新增用户
 * admin/register
 */
const adminRegister = (data: IAdminListItem): Promise<IAdminListItem> => {
  return instacne.post("/admin/register", data);
};

/**
 * /admin/update/{id}
 *  修改指定用户信息
 */
const adminUpdate = (data: IAdminListItem) => {
  return instacne.post(`/admin/update/${data.id}`, data);
};

// 获取index首页的 图表数据接口
const GetIndexDataApi = (): Promise<IIndexStat> => {
  return instacne.get("http://kumanxuan1.f3322.net:8360/admin/stat");
};

//6 资源列表 /resource/list
const resourceListApi = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get("/resource/list", { params: data });
};
// 获取角色相关菜单
const getRoleListMenu = (roleId: number): Promise<any[]> => {
  return instacne.get(`/role/listMenu/${roleId}`);
};
// 根据编号查询品牌信息 /mall-admin/brand/{id}
interface ppinfoRes {
  bigPic: string;
  brandStory: string;
  factoryStatus: number;
  firstLetter: string;
  id: number;
  logo: string;
  name: string;
  productCommentCount: number;
  productCount: number;
  showStatus: number;
  sort: number;
}
// 添加
const updatePP = (id: number): Promise<ppinfoRes> => {
  return instacne.get(`/brand/${id}`);
};

export {
  adminLogin,
  getAdminInfo,
  getAdminList,
  adminRegister,
  adminUpdate,
  GetIndexDataApi,
  resourceListApi,
  getRoleListMenu,
  getresourcesRole,
  getresources,
  updatePP,
};
export type { IAdminLoginParams, IAdminLoginRes, IAdminListItem };
